<?
if (!defined('SMF'))
	die('Hacking attempt...');
	
global $smcFunc, $txt, $scripturl;
global $dbtables, $max_rank;

$txt['bnt_ranks_title'] = str_replace("[max_rank]", $max_rank, $txt['bnt_ranks_title']);
bigtitle($txt['bnt_ranks_title']);

$result = $smcFunc['db_query']('', '
	SELECT COUNT(*) AS num_players
	FROM {raw:db_ships}
	WHERE ship_destroyed="N"
	AND xenobe = "N"',
	array(
		'db_ships' => $dbtables['ships']
	)
);
$row = $smcFunc['db_fetch_assoc']($result);
$num_players = $row['num_players'];
$smcFunc['db_free_result']($result);


$result = $smcFunc['db_query']('', '
	SELECT	{raw:db_ships}.user_id, {raw:db_ships}.score, {raw:db_ships}.character_name, 
			{raw:db_ships}.turns_used, {raw:db_ships}.last_login, UNIX_TIMESTAMP({raw:db_ships}.last_login) as online,
			{raw:db_ships}.rating, {raw:db_teams}.team_name,
			IF({raw:db_ships}.turns_used < 150, 0, ROUND({raw:db_ships}.score / {raw:db_ships}.turns_used)) AS efficiency
	FROM {raw:db_ships}
	LEFT JOIN {raw:db_teams} ON {raw:db_ships}.team = {raw:db_teams}.id
	WHERE ship_destroyed="N"
	AND xenobe = "N"
	ORDER BY {raw:db_ships}.score DESC
	LIMIT {int:max_rank}',
	array(
		'db_ships' => $dbtables['ships'],
		'db_teams' => $dbtables['teams'],
		'max_rank' => $max_rank,
	)
);

if ($smcFunc['db_num_rows']($result) == 0)
	echo $txt['bnt_ranks_none'], '<br>';
else
{
	echo '
		<script type="text/javascript">
			$(document).ready(function(){
				// Table sorting function
				$(".bnt_table").tablesorter({
					widgets: ["zebra"]
				});
			});
		</script>
		<p>', $txt['bnt_ranks_pnum'] ,': <b>', Number($num_players),'</p>
		<p>', $txt['bnt_ranks_dships'], '</p>
		<table class="bnt_table" cellpadding="2">
			<thead>
				<tr>
					<th>', $txt['bnt_ranks_rank'], '</th>
					<th>', $txt['bnt_ranks_score'], '</th>
					<th>', $txt['bnt_player'], '</th>
					<th>', $txt['bnt_ranks_turns'], '</th>
					<th>', $txt['bnt_ranks_lastlog'], '</th>
					<th>', $txt['bnt_ranks_goodevil'], '</th>
					<th>', $txt['bnt_team_alliance'], '</th>
					<th>', $txt['bnt_ranks_online'], '</th>
					<th>', $txt['bnt_ranks_efficiency'], '</th>
				</tr>
			</thead>';

	$i = 0;
	while ($row = $smcFunc['db_fetch_assoc']($result))
	{
		$i++;
		
		$rating = round(sqrt(abs($row['rating'])));
		if (abs($row['rating']) != $row['rating'])
			$rating = -1 * $rating;
		$curtime = TIME();
		$time = $row['online'];
		$difftime = ($curtime - $time) / 60;
		
		$temp_turns = $row['turns_used'];
		
		if ($temp_turns <= 0)
			$temp_turns = 1;
			
		$online = " ";
		
		if ($difftime <= 5)
			$online = "Online";
		echo '
			<tr>
				<td>', Number($i), '</td>
				<td>', Number($row['score']), '</td>
				<td><strong>', $row['character_name'], '</strong> (', player_insignia_name($row['user_id']),')</td>
				<td>', Number($row['turns_used']), '</td>
				<td>', $row['last_login'], '</td>
				<td>', Number($rating), '</td>
				<td>', $row['team_name'], '</td>
				<td>', $online, '</td>
				<td>', $row['efficiency'], '</td>
			</tr>';
	}
	echo '
		</table>';
}

$smcFunc['db_free_result']($result);

if (CheckLogin() == false)
	TextPlayerNotFound();
else
	TextMainMenu();
?>
